﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight;

namespace DynamicChart.UI
{
    public partial class DataSourceView : AppView
    {
        public DataSourceView()
            :base(new Guid("{92C31814-4745-4F26-9E11-0570BB999759}"), "datasource management view")
        {
            InitializeComponent();
        }

        protected override void OnExtendShown()
        {
            if (IsFirstDisplay)
            {
                LoadDataSource();
            }
        }
        #region 加载数据
        private void LoadDataSource()
        {
            ManagementRef.ManagementWebServiceSoapClient client = DynamicChartModule.CreateManagementProxy();
            client.GetInternalDataSourceCompleted += new EventHandler<ManagementRef.GetInternalDataSourceCompletedEventArgs>(client_GetInternalDataSourceCompleted);
            client.GetInternalDataSourceAsync();
        }

        void client_GetInternalDataSourceCompleted(object sender, ManagementRef.GetInternalDataSourceCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _ctlGridDataSource.ItemsSource = e.Result.Data;
            }
        }
        #endregion

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.CreateDataSourceWindow createWindow = new Dialogs.CreateDataSourceWindow();
            createWindow.Closed += new EventHandler(createWindow_Closed);
            createWindow.Show();
        }

        void createWindow_Closed(object sender, EventArgs e)
        {
            Dialogs.CreateDataSourceWindow createWindow = sender as Dialogs.CreateDataSourceWindow;
            if (createWindow.DialogResult == true)
            {
                LoadDataSource();
            }
        }
    }
}
